import { req_tradeInfo, req_user_adress_list, req_submit_order, req_pay_info, req_my_order_data} from '../../api/req_fun'
import { Message } from 'element-ui'
export default {
    namespaced: true,
    state: () => ({
        tradeInfo: {},
        user_adress_list: [],
        pay_info_data: {},
        my_order_data: {}
    }),
    actions: {
        // 获取交易详情
        async get_tradeInfo({commit }) {
            const data = await req_tradeInfo()
            if (data.code === 200) {
                commit('GET_TRADEINFO', data.data)
            } else {
                Message.error('获取数据失败!')
            }
        },
        // 获取用户地址列表
        async get_user_adress_list({commit }) {
            const data = await req_user_adress_list()
            if (data.code === 200) {
                commit('GET_USER_ADRESS_LIST', data.data)
            } else {
                Message.error('获取数据失败!')
            }
        },
        // 提交订单
        async submit_order({ commit }, { req_data, traderNo }) {
            const data = await req_submit_order(req_data, traderNo)
            if (data.code === 200) {
                return data.data
            } else {
                return Promise.reject(new Error('fail'))
            }
        },
        // 获取支付信息 二维码
        async get_pay_info({ commit }, orderId) {
            const data = await req_pay_info(orderId)
            if (data.code === 200) {
                commit('GET_PAY_INFO', data.data)
                return data.data
            } else {
                return Promise.reject(new Error('fail'))
            }
        },
        // 获取我的订单列表
        async get_my_order_data({ commit }, { page, limit}) {
            const data = await req_my_order_data({ page, limit })
            if (data.code === 200) {
                commit('GET_MY_ORDER_DATA', data.data)
                return data.data
            } else {
                return Promise.reject(new Error('fail'))
            }
        }
    },
    mutations: {
        GET_TRADEINFO(state, val) {
            state.tradeInfo = val
        },
        GET_USER_ADRESS_LIST(state, val) {
            state.user_adress_list = val
        },
        GET_PAY_INFO(state, data) {
            state.pay_info_data = data
        },
        GET_MY_ORDER_DATA(state, data) {
            state.my_order_data = data
        }
    },
    getters: {

    }
}